@extends('layouts.app')


@section('title','创建话题')



@section('content')
    <div class="container">
        <div class="tt-wrapper-inner">
            <h1 class="tt-title-border">
                <i class="far fa-edit"></i>
                @if($topic->id)
                    编辑话题
                @else
                    新建话题
                @endif
            </h1>
            @if($topic->id)
                <form class="form-default form-create-topic" action="{{ route('topics.update', $topic->id) }}"
                      method="POST" ACCEPT-CHARSET="UTF-8">
                    <input type="hidden" name="_method" value="PUT">
                    @else
                        <form class="form-default form-create-topic" action="{{ route('topics.store') }}" method="POST"
                              accept-charset="UTF-8">
                            @endif
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">

                            @include('shared._error')

                            <div class="form-group">
                                <label for="title">标题</label>
                                <div class="tt-value-wrapper">
                                    <input type="text" name="title" class="form-control" id="title"
                                           value="{{ old('title',$topic->title) }}"
                                           placeholder="请填写标题" required>
                                    {{--<span class="tt-value-input">99</span>--}}
                                </div>
                                <div class="tt-note">描述好你的主题，同时尽量使主题简短。</div>
                            </div>

                            <div class="form-group">
                                <label for="category">分类</label>
                                <div class="tt-value-wrapper">
                                    <select class="form-control" name="category_id" id="category" required>
                                        <option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>请选择分类</option>
                                        @foreach ($categories as $value)
                                            <option value="{{ $value->id }}" {{ $topic->category_id == $value->id ? 'selected' : '' }}>{{ $value->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="body">内容</label>
                                <div class="tt-value-wrapper">
                                    <textarea name="body" class="form-control" id="editor"
                                              rows="6" placeholder="请填入至少三个字符的内容。"
                                              required>
                                        {{ old('body', $topic->body ) }}
                                    </textarea>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-auto ml-md-auto">
                                    <button type="submit" class="btn btn-secondary btn-width-lg">保存</button>
                                </div>
                            </div>
                        </form>
        </div>
    </div>
@endsection

@section('styles')
    <link rel="stylesheet" type="text/css" href="{{ asset('css/simditor.css') }}">
@stop

@section('scripts')
    <script type="text/javascript" src="{{ asset('js/module.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/hotkeys.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/uploader.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/simditor.js') }}"></script>

    <script>
        $(document).ready(function () {
            var editor = new Simditor({
                textarea: $('#editor'),
                upload:{
                    url:'{{ route('topics.upload_image') }}',
                    params:{
                        _token:'{{ csrf_token() }}'
                    },
                    fileKey: 'upload_file',
                    connectionCount: 10,
                    leaveConfirm: '文件上传中，关闭此页面将取消上传。'
                },
                pasteImage:true,
            });
        });
    </script>
@stop